package com.example.applicationlifecycle;

import android.app.Application;
import android.content.Context;

import com.lws.applicationlifecycle.AppLifeCycle;

public class MainApp extends Application {


    private final AppLifeCycle appLifeCycle = new AppLifeCycle();


    //提供AppLifeCycle服务
    @Override
    public Object getSystemService(String name) {
        if (name.equals(AppLifeCycle.NAME)) {
            return appLifeCycle;
        }
        return super.getSystemService(name);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

        //初始化SubApp1(AppLifeCycleObserver)
        new SubApp1().attachBaseContext(this);

        //初始化SubApp2(Application)
        new SubApp2().attachBaseContext(this);


        //初始化SubApp3(Application)
        appLifeCycle.addAppLifeCycleObserver(new SubApp3(this));

    }

    @Override
    public void onCreate() {
        super.onCreate();
        appLifeCycle.onCreate();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        appLifeCycle.onTerminate();
    }
}
